package com.etc.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.etc.entity.Document;
import com.etc.service.DocumentService;
import com.etc.util.AbsResponseData;
import com.etc.util.ResponseData;
import com.github.pagehelper.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 
 * @ClassName: DocumentController
 * @Description: @RestController = @Controller + @ResponseBody
 * @author knowno
 * @date 2022年4月7日 上午11:04:24
 *
 */
@RestController
@Api(value = "文档管理控制器", description = "文档管理接口")
public class DocumentController {
	Logger logger = LogManager.getLogger(DeptController.class);

	// 注入一个service
	/** 
	* @Fields service : TODO(用一句话描述这个变量表示什么) 
	*/ 
	@Autowired
	private DocumentService service;
	
	@PostMapping(value = "document")
	@ApiOperation(value = "增加", httpMethod = "POST", notes = "新增操作", response = AbsResponseData.class)
	public AbsResponseData add(@RequestBody Document document) {
		int result = service.add(document);

		logger.info("info :" + result);
		AbsResponseData ard = new AbsResponseData();
		if (result != 1) {
			ard.setMsg("fail");
		}

		return ard;

	}

	/**
	 * 
	 * @param page     页码
	 * @param limit    每页记录数
	 * @param keywords 查询关键字
	 * @return
	 */
	@GetMapping(value = "document")
	@ApiOperation(value = "文档的模糊查询", httpMethod = "GET", notes = "文档模糊查询方法", response = ResponseData.class)
	public ResponseData<Document> getDocument(@RequestParam(value = "page", required = true, defaultValue = "1") Integer page,
			@RequestParam(value = "limit", required = true, defaultValue = "10") Integer limit,
			@RequestParam(value = "keywords", required = true, defaultValue = "") @ApiParam(value = "关键字", name = "keywords", required = false) String keywords) {
		Page<Document> pages = service.getByPage(page, limit, keywords);

		ResponseData<Document> rdata = new ResponseData<Document>(pages.getTotal(), pages.getResult());

		return rdata;
	}

}
